package captcha

import (
	"fmt"
	"github.com/mojocn/base64Captcha"
	"image/color"
	"math/rand"
)

type Math struct {
	*base64Captcha.DriverMath
}

// NewDriverMath creates a driver of math
func NewDriverMath(height int, width int, noiseCount int, showLineOptions int, bgColor *color.RGBA, fontsStorage base64Captcha.FontsStorage, fonts []string) *Math {
	driver := base64Captcha.NewDriverMath(height, width, noiseCount, showLineOptions, bgColor, fontsStorage, fonts)
	return &Math{DriverMath: driver}
}

// GenerateIdQuestionAnswer creates id,captcha content and answer
func (d *Math) GenerateIdQuestionAnswer() (id, question, answer string) {
	id = base64Captcha.RandomId()
	operators := []string{"+", "-", "x"}
	var mathResult int32
	switch operators[rand.Int31n(3)] {
	case "+":
		a := rand.Int31n(10)
		b := rand.Int31n(10)
		question = fmt.Sprintf("%d+%d=?", a, b)
		mathResult = a + b
	case "x":
		a := rand.Int31n(10)
		b := rand.Int31n(10)
		question = fmt.Sprintf("%dx%d=?", a, b)
		mathResult = a * b
	default:
		a := rand.Int31n(10)
		b := rand.Int31n(10)
		question = fmt.Sprintf("%d-%d=?", max(a, b), min(a, b))
		mathResult = max(a, b) - min(a, b)

	}
	answer = fmt.Sprintf("%d", mathResult)
	return
}
